package com.lingchen.boot.utils;

import java.security.Key;
import java.security.MessageDigest;

import javax.crypto.Cipher;
import javax.crypto.spec.IvParameterSpec;
import javax.crypto.spec.SecretKeySpec;

import org.springframework.util.Base64Utils;

public class EasyAes {
    private static final String ALGORITHM = "AES";
    private static final IvParameterSpec DEFAULT_IV = new IvParameterSpec(new byte[]{(byte) 0, (byte) 0, (byte) 0, (byte) 0, (byte) 0, (byte) 0, (byte) 0, (byte) 0, (byte) 0, (byte) 0, (byte) 0, (byte) 0, (byte) 0, (byte) 0, (byte) 0, (byte) 0});
    public static final String PWD = "xujikmlioksjoped";
    private static final String TRANSFORMATION = "AES/ECB/PKCS5Padding";
    private Cipher cipher;
    private IvParameterSpec iv;
    private Key key;

    public EasyAes(String str) {
        this(str, 128);
    }

    public EasyAes(String str, int i) {
        this(str, i, null);
    }

    public EasyAes(String str, int i, String str2) {
        this.key = new SecretKeySpec(str.getBytes(), "AES");
        if (str2 != null) {
            this.iv = new IvParameterSpec(getHash("MD5", str2));
        } else {
            this.iv = DEFAULT_IV;
        }
        init();
    }

    public static String decryptString(String str) {
        String str2 = null;
        try {
            str2 = new EasyAes("xujikmlioksjoped", 128).decrypt(str);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return str2;
    }

    public static String encryptString(String str) {
        String str2 = null;
        try {
            str2 = new EasyAes("xujikmlioksjoped", 128).encrypt(str);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return str2;
    }

    private static byte[] getHash(String str, String str2) {
        try {
            return getHash(str, str2.getBytes("UTF-8"));
        } catch (Exception e) {
            throw new RuntimeException(e.getMessage());
        }
    }

    private static byte[] getHash(String str, byte[] bArr) {
        try {
            MessageDigest instance = MessageDigest.getInstance(str);
            instance.update(bArr);
            return instance.digest();
        } catch (Exception e) {
            throw new RuntimeException(e.getMessage());
        }
    }

    private void init() {
        try {
            this.cipher = Cipher.getInstance("AES/ECB/PKCS5Padding");
        } catch (Exception e) {
            throw new RuntimeException(e.getMessage());
        }
    }

    public String decrypt(String str) {
        try {
            return decrypt(Base64Utils.decodeFromString(str));
        } catch (Exception e) {
            throw new RuntimeException(e.getMessage());
        }
    }

    public String decrypt(byte[] bArr) {
        try {
            this.cipher.init(2, this.key);
            return new String(this.cipher.doFinal(bArr), "UTF-8");
        } catch (Exception e) {
            throw new RuntimeException(e.getMessage());
        }
    }

    public String encrypt(String str) {
        try {
            return encrypt(str.getBytes("UTF-8"));
        } catch (Exception e) {
            throw new RuntimeException(e.getMessage());
        }
    }

    public String encrypt(byte[] bArr) {
        try {
            this.cipher.init(1, this.key);
            byte[] doFinal = this.cipher.doFinal(bArr);
//            a.e(Integer.valueOf(doFinal.length));
            return new String(Base64Utils.encode(doFinal), "UTF-8");
        } catch (Exception e) {
            throw new RuntimeException(e.getMessage());
        }
    }
}
